package com.david.webtest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.david.core.imgloader.LruImageLoader;
import com.david.core.imgloader.LruImageLoaderUtils;

import java.io.InputStream;

import androidx.annotation.Nullable;


/***
 * html 向native传递数据
 * 1：自定义scheme方式，在native拦截url判断是否应该处理；
 * 2：通过html调用alert、confirm、prompt方法，native拦截方法并重新逻辑
 * 3：通过定义jsBrige方式（android），JS注入对象（iOS），html调用相关方法
 *
 * native向html传递数据：
 * 1：html中定义js方法，native通过注入js调用方法传递
 */
public class WebActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_david_web);
        LruImageLoader.getInstance().init(getApplication());

        WebView mWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
//        webSettings.setWebChromeClient(webChromeClient);
//        webSettings.setWebViewClient(webViewClient);
        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                return super.onJsConfirm(view, url, message, result);
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                return super.onJsPrompt(view, url, message, defaultValue, result);
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {


            @Override
            public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest request) {
                String url = request.getUrl().toString();
//                try {
                    if (url.contains(".png") || url.endsWith(".jpg") ||url.endsWith(".jpeg") || url.endsWith(".webp") || url.endsWith(".NEF")) {
//                        @SuppressLint("ResourceType") InputStream is = getResources().openRawResource(R.mipmap.ic_logo);
                        InputStream is = null;
                        try {
                            if (LruImageLoaderUtils.isImgCached(url)) {
                                is = LruImageLoaderUtils.getImgCacheStream(url);
                                WebResourceResponse response = new WebResourceResponse("image/png",
                                        "utf-8", is);
                                Log.e("img", "imgUrl is:" + url);

                                return response;
                            } else {
                                LruImageLoader.getInstance().loadCacheImage(url, null);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }
//                    else if (url.endsWith("jquery.min.js")) {
//                        InputStream is = appRm.getInputStream(R.raw.jquery_min_js);
//                        WebResourceResponse response = new WebResourceResponse("text/javascript",
//                                "utf-8", is);
//                        return response;
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
                return super.shouldInterceptRequest(webView, request);
            }
        });

        mWebView.loadUrl("https://www.dealmoon.com/cn/real-time-chinese-translation-apple-special-events-live-broadcast-including-iphone-release-wwdc-etc/1829188.html");
    }


    private WebResourceResponse getWebResourceResponse(String url, String mime, String style) {
        WebResourceResponse response = null;
//        try {
//            response = new WebResourceResponse(mime, "UTF-8", new FileInputStream(new File(getJSPath() + TPMD5.md5String(url) + style)));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
        return response;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
